Skip to content


ai  101  pytorch  classification  nvidia  cuda  install  tensorrt  yolo  ardupilot  None  ros2  dds  micro ros  xrce  sitl  plugin  SITL  debug  rangefinder  pymavlink  mavros  gazebo  distance sensor  system_time  timesync  cmake  gtest  ctest  cpp  c++  format  fmt  multithreading  spdlog  camera  coordinate system  orb  matching  opencv  build  transformation  computer vision  homography  optical flow  of  trackers  cv  cyclonedds  eprosima  fastdds  simulation  config  ignition  bridge  sdf  tips  ign-transport  sensors  lidar  aptly  apt  encryption  pgp  docker  git  bundle  github  hooks  pre-commit  lxd  container  lxc  x11  profile  vscode  marpit  presentation  marp  markdown  mermaid  video  ffmpeg  gstreamer  cheat-sheet  sdp  v4l2loopback  gi  snippets  cheat Sheet  python  asyncio  future  click  cli  numpy  project  template  black  isort  docs  project document  docstrings  flake8  linter  git-hook  mypy  unittest  pytest  pylint  mock  iterator  generator  logging  tuple  namedtuple  typing  annotation  pyzmq  zmq  msgpack  action  namespace  remap  control2  ros2_control  gdb  qos  tag  plugins  msg  node  zero-copy  shm  tutorial  algorithm  calibration  diff  pid  dev  colcon  colcon_cd  rpi  arm  qemu  settings  behavior  plot  visualization  debugging  diagnostic  diagnostics  tutorials  gst  math  apm  rat_runtime_monitor  web  rosbridge  vue  binding  discovery  gazebo-classic  launch  spawn  cook  gps  imu  ray  gazebo_ros_ray_sensor  ultrsonic  range  ultrasonic  gazebo classic  wrench  effort  odom  ign  gz  xacro  ros_ign  diff_drive  odometry  joint_state  argument  OpaqueFunction  DeclareLaunchArgument  LaunchConfiguration  tmux  nav  slam  test  rclpy  executor  MultiThreadedExecutor  SingleThreadedExecutor  param  dynamic-reconfigure  service  client  setup.py  package.xml  parameter  parameters  custom  msgs  executers  pub  sub  rqt  rviz  rviz2  pose  marker  tf2  deb  package  setup  local_setup  rosdep  package manager  project settings  vcstool  cross-compiler  nano  texture  tmuxp  rootfs  embedded  zah  linux  rm  ubuntu  ip  ss  network  netstat  snap  deploy  ssh  systemd  mkdocs  extensions  socat  networking  serial  udp  tc  mtu  select  px4  robotics  kalman_filter  kalman  filter  control  todo  vscode-ext  json  yaml  schema  yocto  poky  world  gazebo_ros2_control  position_controller  effort_controller  velocity_controller  urdf  gazebo_ros_force  gazebo_ros_joint_state_publisher  robot_state_publisher  joint_state_publisher  projects  vrx  buoyancy 

Python project template - pre-commit#

Demo of usage pre-commit tool

Install pre-commit tool and config it with four tools

  • isort
  • black
  • mypy
  • flake8

pre-commit#

  • Install pre-commit
  • Add config file
  • Install hooks
  • Add hooks
  • Run
install
python -m pip install pre-commit
config
touch .pre-commit-config.yaml
install hook
pre-commit install

hooks#

isort#

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.

  • config pre-commit
  • add isort config to pyproject.toml
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/PyCQA/isort
    rev: 5.12.0
    hooks:
      - id: isort
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a

config with black

config isort with black profile


black#

Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place.

  • config pre-commit
  • add isort config to pyproject.toml
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        language_version: python3.10
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.isort]
profile = "black"
install and run
pre-commit install
# Run
pre-commit run -a

config isort

config isort with black profile

black and isort format import different, if both config as pre-commit action they interrupt each other

disable fmt format for code section

# fmt: off
...
# fmt: on

mypy#

  • config pre-commit
  • add mypy section to pyproject.toml config
  • install and run
.pre-commit-config.yaml
repos:
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.942
    hooks:
      - id: mypy
pyproject.toml
[settings]
[tool.black]
line-length = 120

[tool.mypy]

add inline ignore

# type: ignore

flake8#

  • config pre-commit
  • add .flake8 config

code style checker for PEP8

.pre-commit-config.yaml
repos:
  - repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
      - id: flake8
.flake8
[flake8]
max-line-length = 120
max-complexity = 10
exclude=src/apm_demos/test/*

Recap#

  • isort, black and mypy config from myproject.toml
  • flake8 config with its on file .flake8
  • isort must be config with black profile

disabled pre-commit

git commit --no-verify

.pre-commit-config.yaml#

files: src/apm_demos
repos:
  - repo: https://github.com/PyCQA/isort
    rev: 5.12.0
    hooks:
      - id: isort
  - repo: https://github.com/psf/black
    rev: 23.3.0
    hooks:
      - id: black
        language_version: python3.10
  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.942
    hooks:
      - id: mypy
  - repo: https://github.com/PyCQA/flake8
    rev: 6.0.0
    hooks:
      - id: flake8

Reference#